package com.makeunion.gdlbsdemo.activity;

import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.makeunion.gdlbsdemo.R;

import java.util.List;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class MyLocationActivity extends AppCompatActivity implements AMap.OnMyLocationChangeListener,
        GeocodeSearch.OnGeocodeSearchListener {

    private MapView mapView;

    private AMap aMap;

    private MyLocationStyle myLocationStyle;

    private GeocodeSearch geoSearch;

    private double longitude;

    private double latitude;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_location);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        aMap = mapView.getMap();
        setupMapView();
        setupLocationStyle();

        geoSearch = new GeocodeSearch(this);
        geoSearch.setOnGeocodeSearchListener(this);
    }

    private void setupMapView() {
        // 设置默认定位按钮是否显示，非必需设置
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        // 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false
        aMap.setMyLocationEnabled(true);
        aMap.setOnMyLocationChangeListener(this);
        aMap.showIndoorMap(true);
    }

    /**
     * 设置自定义定位蓝点
     */
    private void setupLocationStyle() {
        myLocationStyle = new MyLocationStyle();
        // 默认模式，连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动，1秒1次定位
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
        // 设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒
        myLocationStyle.interval(1000);
        // 设置定位蓝点的Style
        aMap.setMyLocationStyle(myLocationStyle);
    }

    @Override
    public void onMyLocationChange(Location location) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
        Log.e("TAG", "MyLocation=[" + location.getLongitude() + ", " + location.getLatitude() + "]");
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult result, int code) {
        Log.e("TAG", "code=" + code);
        if (result == null) {
            Log.e("TAG", "result为空");
            return;
        }
        RegeocodeAddress address = result.getRegeocodeAddress();
        if (address == null) {
            Log.e("TAG", "地址为空");
        } else {
            Log.e("TAG", "地址：" + address.getProvince() + address.getCity() + address.getDistrict());
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult result, int code) {
        Log.e("TAG", "code=" + code);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.type_show:
                // 只定位一次
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);
                break;
            case R.id.type_locate:
                // 定位一次，且将视角移动到地图中心点
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;
                break;
            case R.id.type_follow:
                // 连续定位、且将视角移动到地图中心点，定位蓝点跟随设备移动，1秒1次定位
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW) ;
                break;
            case R.id.type_map_rotate:
                // 连续定位、且将视角移动到地图中心点，地图依照设备方向旋转，定位点会跟随设备移动，1秒1次定位
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);
                break;
            case R.id.type_location_rotate:
                // 默认模式：连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动，1秒1次定位
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
                break;
            case R.id.type_location_rotate_no_center:
                // 5.1.0版本开始提供
                // 连续定位、蓝点不会移动到地图中心点，定位点依照设备方向旋转，并且蓝点会跟随设备移动
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
                break;
            case R.id.type_follow_no_center:
                // 5.1.0版本开始提供
                // 连续定位、蓝点不会移动到地图中心点，并且蓝点会跟随设备移动
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);
                break;
            case R.id.type_map_rotate_no_center:
                // 5.1.0版本开始提供
                // 连续定位、蓝点不会移动到地图中心点，地图依照设备方向旋转，并且蓝点会跟随设备移动
                myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE_NO_CENTER);
                break;
        }
        return true;
    }

    public void showLocatePoint(View view) {
        myLocationStyle.showMyLocation(!myLocationStyle.isMyLocationShowing());
    }

    public void customLocationIcon(View view) {
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.custom_location_icon);
        myLocationStyle.myLocationIcon(bitmapDescriptor);
        aMap.setMyLocationStyle(myLocationStyle);
    }

    public void iconAnchor(View view) {
        // 图标的左上角对应定位点
        myLocationStyle.anchor(0.0f, 1.0f);
        aMap.setMyLocationStyle(myLocationStyle);
    }

    public void circleDisplay(View view) {
        myLocationStyle.strokeColor(Color.RED);
        myLocationStyle.radiusFillColor(Color.LTGRAY);
        myLocationStyle.strokeWidth(2);
        aMap.setMyLocationStyle(myLocationStyle);
    }

    public void queryProvinceCity(View view) {
        // 第一个参数表示一个Latlng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
        LatLonPoint point = new LatLonPoint(longitude, latitude);
        RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP);
        geoSearch.getFromLocationAsyn(query);
    }

    public void queryLongLat(View view) {
        // name表示地址，第二个参数表示查询城市，中文或者中文全拼，citycode、adcode
        GeocodeQuery query = new GeocodeQuery("北京市", "010");
        geoSearch.getFromLocationNameAsyn(query);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        aMap.setOnMyLocationChangeListener(null);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}
